package com.hua.common.photopicker.view;

import android.app.Activity;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.hua.common.R;
import com.hua.common.commontools.DipUtil;
import com.hua.common.photopicker.util.PhotoDirectory;
import java.util.List;

public class PhotoListAdapter extends BaseAdapter {

    private Activity mContext = null;
    private List<PhotoDirectory> list = null;
    private LayoutInflater layoutInflater = null;
    private int width = 0;

    public PhotoListAdapter(Activity context, List<PhotoDirectory> list) {
        this.mContext = context;
        layoutInflater = LayoutInflater.from(mContext);
        this.list = list;
        this.width = DipUtil.Dp2px(mContext, 65);
    }

    @Override
    public int getCount() {
        if (list != null) {
            return list.size();
        } else {
            return 0;
        }
    }

    @Override
    public PhotoDirectory getItem(int position) {

        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View converView, ViewGroup arg2) {
        Holder holder = null;
        if (converView == null) {
            converView = layoutInflater.inflate(R.layout.photolistitem, null);
            holder = new Holder();
            holder.img = (ImageView) converView.findViewById(R.id.img);
            holder.nameTv = (TextView) converView.findViewById(R.id.tvname);
            converView.setTag(holder);
        } else {
            holder = (Holder) converView.getTag();
        }
        String path = list.get(position).getCoverPath();
        Uri uri = Uri.parse("file://" + path);
        Glide.with(mContext)
                .load(uri)
                .placeholder(R.drawable.ic_photo_black_48dp)
                .error(R.drawable.ic_broken_image_black_48dp)
                .crossFade()
                .centerCrop()
                .into(holder.img);
        holder.nameTv.setText("");
        holder.nameTv.setText(list.get(position).getName());
        return converView;
    }

    class Holder {
        private ImageView img = null;
        private TextView nameTv = null;
    }
}
